package dk.muldbjergskolen.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;


import dk.muldbjergskolen.ctr.ElevCtr;
import dk.muldbjergskolen.ctr.KlasseCtr;
import dk.muldbjergskolen.db.PersonDB.UGyldigtPostNr;
import dk.muldbjergskolen.model.Klasse;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

public class OpretElev extends JDialog
{
	private final JPanel contentPanel = new JPanel();
	private JTextField textField_Fornavn;
	private JTextField textField_Efternavn;
	private JTextField textField_Adresse;
	private JTextField textField_PostNr;
	private JTextField textField_Telefon;
	private JTextField textField_Cpr;
	private JTextField textField_Email;
	private JComboBox<Klasse> cbKlasse;
	private JFrame frame;
	private ElevCtr eC = new ElevCtr();
	private KlasseCtr kC = new KlasseCtr();

	/**
	 * Create the dialog.
	 */
	public OpretElev(OpretElevListener listener)
	{
		opretElevListener = listener;
		
		setModalityType(ModalityType.APPLICATION_MODAL);
		setTitle("Opret Elev");
		setBounds(100, 100, 329, 303);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel label = new JLabel("Fornavn:");
		label.setBounds(10, 14, 80, 14);
		contentPanel.add(label);
		
		textField_Fornavn = new JTextField();
		textField_Fornavn.setColumns(10);
		textField_Fornavn.setBounds(100, 11, 200, 20);
		contentPanel.add(textField_Fornavn);
		
		JLabel label_1 = new JLabel("Efternavn:");
		label_1.setBounds(10, 42, 80, 14);
		contentPanel.add(label_1);
		
		textField_Efternavn = new JTextField();
		textField_Efternavn.setColumns(10);
		textField_Efternavn.setBounds(100, 39, 200, 20);
		contentPanel.add(textField_Efternavn);
		
		JLabel label_2 = new JLabel("Adresse:");
		label_2.setBounds(10, 70, 80, 14);
		contentPanel.add(label_2);
		
		textField_Adresse = new JTextField();
		textField_Adresse.setColumns(10);
		textField_Adresse.setBounds(100, 67, 200, 20);
		contentPanel.add(textField_Adresse);
		
		JLabel label_3 = new JLabel("Postnr:");
		label_3.setBounds(10, 98, 80, 14);
		contentPanel.add(label_3);
		
		textField_PostNr = new JTextField();
		textField_PostNr.setColumns(10);
		textField_PostNr.setBounds(100, 95, 200, 20);
		contentPanel.add(textField_PostNr);
		
		JLabel label_5 = new JLabel("Telefon:");
		label_5.setBounds(10, 126, 80, 14);
		contentPanel.add(label_5);
		
		textField_Telefon = new JTextField();
		textField_Telefon.setColumns(10);
		textField_Telefon.setBounds(100, 123, 200, 20);
		contentPanel.add(textField_Telefon);
		
		JLabel label_7 = new JLabel("Email:");
		label_7.setBounds(10, 154, 80, 14);
		contentPanel.add(label_7);
		
		JLabel label_8 = new JLabel("CPR:");
		label_8.setBounds(10, 182, 80, 14);
		contentPanel.add(label_8);
		
		textField_Cpr = new JTextField();
		textField_Cpr.setColumns(10);
		textField_Cpr.setBounds(100, 179, 200, 20);
		contentPanel.add(textField_Cpr);
		
		textField_Email = new JTextField();
		textField_Email.setColumns(10);
		textField_Email.setBounds(100, 151, 200, 20);
		contentPanel.add(textField_Email);
		
		JLabel lblKlasse = new JLabel("Klasse:");
		lblKlasse.setBounds(10, 207, 80, 14);
		contentPanel.add(lblKlasse);
		
		

		cbKlasse = new JComboBox<Klasse>();
		cbKlasse.setBounds(100, 204, 200, 20);
		contentPanel.add(cbKlasse);
		opdaterKlasser();
		
		
		
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						opret();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	private void opdaterKlasser()
	{
		try
		{
			cbKlasse.removeAllItems();
			ArrayList<dk.muldbjergskolen.model.Klasse> b = kC.findKlasser();

			for (int i = 0; i < b.size(); i++)
			{
				cbKlasse.addItem(b.get(i));
			}
		}

		catch (Exception e)
		{
			JOptionPane.showMessageDialog(this, "Ingen klasser blev fundet.");
		}
	}
	
	private void opret()
	{
		String fornavn = textField_Fornavn.getText();
		String efternavn = textField_Efternavn.getText();
		String addresse = textField_Adresse.getText();
		String tlf = textField_Telefon.getText();
		String email = textField_Email.getText();
		String cpr = textField_Cpr.getText();
		Klasse klasse = cbKlasse.getItemAt(cbKlasse.getSelectedIndex());
		
		try
		{
			int postNr = Integer.parseInt(textField_PostNr.getText());
			eC.opretElev(fornavn, efternavn, addresse, postNr, tlf, email, cpr, klasse);
		}
		catch (UGyldigtPostNr | NumberFormatException e)
		{
			JOptionPane.showMessageDialog(this, "Ugyldigt post nr. Prøv igen.");
			return;
		}
		catch (Exception e)
		{
			JOptionPane.showMessageDialog(frame, "Udfyld alle Felterne");
			return;
		}
		OnNewElev();
		setVisible(false);
	}
	
	
	
	//
	// Event håndtering.
	//

	// "Lyttere"
	OpretElevListener opretElevListener;

	public interface OpretElevListener
	{
		void OnNewElev();
	}

	/***
	 * Kaldes når en ny Elev er oprettet.
	 */
	private void OnNewElev()
	{
		if (opretElevListener != null)
			opretElevListener.OnNewElev();
	}
}
